<template>
  <div v-if="visible" class="confirm-dialog-overlay" @click="handleOverlayClick">
    <div class="confirm-dialog" @click.stop>
      <div class="confirm-dialog-header">
        <h3 class="confirm-dialog-title">{{ title }}</h3>
        <button class="confirm-dialog-close" @click="handleCancel">
          <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M8 6.586L12.95 1.636L14.364 3.05L9.414 8L14.364 12.95L12.95 14.364L8 9.414L3.05 14.364L1.636 12.95L6.586 8L1.636 3.05L3.05 1.636L8 6.586Z" fill="currentColor" />
          </svg>
        </button>
      </div>
      <div class="confirm-dialog-content">
        <p class="confirm-dialog-message">{{ message }}</p>
      </div>
      <div class="confirm-dialog-footer">
        <button
          class="confirm-dialog-cancel"
          :disabled="loading"
          @click="handleCancel"
        >
          {{ cancelText }}
        </button>
        <button
          class="confirm-dialog-confirm"
          :disabled="loading"
          :class="{ 'loading': loading }"
          @click="handleConfirm"
        >
          <span v-if="loading" class="confirm-loading-icon"></span>
          {{ confirmText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ConfirmDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '确认'
    },
    message: {
      type: String,
      default: '确定要执行此操作吗？'
    },
    confirmText: {
      type: String,
      default: '确认'
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    loading: {
      type: Boolean,
      default: false
    },
    closeOnOverlayClick: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    handleConfirm() {
      this.$emit('confirm');
    },
    handleCancel() {
      this.$emit('cancel');
    },
    handleOverlayClick() {
      if (this.closeOnOverlayClick) {
        this.handleCancel();
      }
    }
  }
}
</script>

<style scoped>
.confirm-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.confirm-dialog {
  width: 320px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.confirm-dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.confirm-dialog-title {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  color: #262626;
}

.confirm-dialog-close {
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
  color: #8c8c8c;
  display: flex;
  align-items: center;
  justify-content: center;
}

.confirm-dialog-close:hover {
  color: #262626;
}

.confirm-dialog-content {
  padding: 20px;
}

.confirm-dialog-message {
  margin: 0;
  font-size: 14px;
  color: #595959;
}

.confirm-dialog-footer {
  display: flex;
  justify-content: flex-end;
  padding: 12px 20px;
  border-top: 1px solid #f0f0f0;
}

.confirm-dialog-cancel,
.confirm-dialog-confirm {
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  border: none;
  outline: none;
}

.confirm-dialog-cancel {
  background-color: white;
  color: #595959;
  border: 1px solid #d9d9d9;
  margin-right: 8px;
}

.confirm-dialog-cancel:hover {
  color: #1890ff;
  border-color: #1890ff;
}

.confirm-dialog-confirm {
  background-color: #1890ff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.confirm-dialog-confirm:hover {
  background-color: #40a9ff;
}

.confirm-dialog-confirm:active {
  background-color: #096dd9;
}

.confirm-dialog-confirm[disabled],
.confirm-dialog-cancel[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

.confirm-loading-icon {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid white;
  border-radius: 50%;
  margin-right: 8px;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style> 